$(function() {
	var loading = weui.loading('loading', {
		className: 'custom-classname',
	});
	var orderId = Common.fn.getUrlParam('oid');
	if (!orderId) {
		location.href = '../views/mine.html';
	}
	var result = Model.gift.orderDetail({
		orderId: orderId,
	});
	if (!sessionStorage.getItem('hmdj_stopError')) {
		sessionStorage.setItem('hmdj_stopError', true);
	} else {
		if (result.code) {
			Common.fn.formatOutput(result);
			loading.hide();
			return false;
		}
    }
    var res = result.data[0];
	// 设置页面信息
	$('.order-status').text(res.orderInfo.state);
	$('.order-id').text(res.orderInfo.orderId);
	$('.order-count').text(res.orderInfo.price + '红米');
	$('.user-nickname').text(res.orderInfo.receiver);
	$('.user-phone').text(res.orderInfo.phone);
	$('.user-address').text(res.orderInfo.address);
	$('.portrait-img').attr('src', res.giftInfo.picture);
	$('.order-good-title').text(res.giftInfo.name);
	$('.goods-price').text(res.orderInfo.price + '红米');
	$('.toShopBtn').attr('href', 'gift_detail.html?id=' + res.giftInfo.giftId);
	switch (res.orderInfo.state) {
		case '已兑换':
			$('.wuliu-order').text('暂无');
            $('.wuliu-copy-btn').css('display', 'none');
            $('.sure-send-btn').attr('data-id', res.orderInfo.orderId);
			break;
		case '已发货':
            $('.footer-cancel-send').css('display', 'none');
            $('.wuliu-order').text(res.orderInfo.express + ' ' + res.orderInfo.expressOrder);
			break;
		case '已完成':
			$('.footer-cancel-send').css('display', 'none');
			$('.wuliu-order').text(res.orderInfo.express + ' ' + res.orderInfo.expressOrder);
			break;
		case '已废弃':
			$('.footer-cancel-send').css('display', 'none');
			$('.wuliu-order').text(res.orderInfo.express + ' ' + res.orderInfo.expressOrder);
			break;
	}
	loading.hide();
});

// 复制订单编号
$('#copy-orderId-btn').on('click', function() {
	var orderId = $('.order-id').text();
	var clipboard = new Clipboard('#copy-orderId-btn', {
		text: function text() {
			return orderId;
		},
	});
	clipboard.on('success', function(e) {
		weui.toast('已成功复制到剪贴板', {
			duration: 2000,
			callback: function callback() {},
		});
	});
	clipboard.on('error', function(e) {
		weui.topTips('复制剪贴板失败，请手动复制');
	});
});

// 复制快递信息
$('#wuliu-copy-btn').on('click', function() {
	var wuliu = $('.wuliu-order').text();
	var clipboard = new Clipboard('#wuliu-copy-btn', {
		text: function text() {
			return wuliu;
		},
	});
	clipboard.on('success', function(e) {
		weui.toast('已成功复制到剪贴板', {
			duration: 2000,
			callback: function callback() {},
		});
	});
	clipboard.on('error', function(e) {
		weui.topTips('复制剪贴板失败，请手动复制');
	});
});

// 确认发货按钮
$(document).on('click', '.sure-send-btn', function() {
	$('.order-form').removeClass('hide');
	$('.order-btn-submit').attr('data-id', $(this).attr('data-id'));
});

// 取消按钮
$(document).on('click', '.order-btn-quit', function() {
	$('.order-form').addClass('hide');
});

// 选择快递信息
$(document).on('click', '#express-company', function() {
	$('.classify-block').css('display', 'block');
});

// 选择快递关闭按钮
$(document).on('click', '.classify-close-btn', function() {
	$('.classify-block').css('display', 'none');
});

// 选择快递公司
$(document).on('click', '.start-classify-item', function() {
	var id = $(this).attr('data-id');
	$('.classify-block').css('display', 'none');
	$('#express-company').val(id);
});

// 确认按钮
$(document).on('click', '.order-btn-submit', function() {
	var company = $.trim($('#express-company').val()),
		number = $.trim($('#express-number').val()),
        id = $(this).attr('data-id');
	if (company == '' || number == '') {
		weui.alert('请填写快递信息');
		return false;
	}
	var confirms = weui.confirm(
		'确认发货吗？',
		function() {
			var result = Model.gift.deliverGift({ orderId: id, express: company, expressOrder: number });
			if (result.code) {
				alert(result.message);
				return false;
			}
			alert(result.data);
			location.reload();
		},
		function() {}
	);
});
